package com.example.dormitorymanagement.controller;

import com.example.dormitorymanagement.model.Occupancy;
import com.example.dormitorymanagement.service.OccupancyService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/occupancies")
@RequiredArgsConstructor
public class OccupancyController {

    private final OccupancyService occupancyService;

    @PostMapping("/check-in")
    public ResponseEntity<Occupancy> checkIn(
            @RequestParam Integer studentId,
            @RequestParam Integer roomId) {
        Occupancy occupancy = occupancyService.checkIn(studentId, roomId);
        return ResponseEntity.status(HttpStatus.CREATED).body(occupancy);
    }

    @PostMapping("/{id}/check-out")
    public ResponseEntity<Occupancy> checkOut(@PathVariable Integer id) {
        Occupancy occupancy = occupancyService.checkOut(id);
        return ResponseEntity.ok(occupancy);
    }

    @GetMapping("/students/{studentId}")
    public ResponseEntity<List<Occupancy>> getOccupanciesByStudent(@PathVariable Integer studentId) {
        List<Occupancy> occupancies = occupancyService.getOccupanciesByStudent(studentId);
        return ResponseEntity.ok(occupancies);
    }

    @GetMapping("/rooms/{roomId}/current")
    public ResponseEntity<List<Occupancy>> getCurrentOccupanciesByRoom(@PathVariable Integer roomId) {
        List<Occupancy> occupancies = occupancyService.getCurrentOccupanciesByRoom(roomId);
        return ResponseEntity.ok(occupancies);
    }

    @GetMapping("/students/{studentId}/current")
    public ResponseEntity<Occupancy> getCurrentOccupancyByStudent(@PathVariable Integer studentId) {
        Occupancy occupancy = occupancyService.getCurrentOccupancyByStudent(studentId);
        return ResponseEntity.ok(occupancy);
    }
}